7 * @covers SearchEngine<extended>
8 * @note Coverage will only ever show one of on of the Search* classes
10 class SearchEngineTest
extends MediaWikiLangTestCase
{
18 * Checks for database type & version.
19 * Will skip current test if DB does not support search.
21 protected function setUp() {
24 // Search tests require MySQL or SQLite with FTS
25 $dbType = $this->db
->getType();
26 $dbSupported = ( $dbType === 'mysql' )
27 ||
( $dbType === 'sqlite' && $this->db
->getFulltextSearchModule() == 'FTS3' );
29 if ( !$dbSupported ) {
30 $this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
33 $searchType = SearchEngineFactory
::getSearchEngineClass( $this->db
);
34 $this->setMwGlobals( [
35 'wgSearchType' => $searchType,
36 'wgCapitalLinks' => true,
37 'wgCapitalLinkOverrides' => [
38 NS_CATEGORY
=> false // for testCompletionSearchMustRespectCapitalLinkOverrides
42 $this->search
= new $searchType( $this->db
);
45 protected function tearDown() {
46 unset( $this->search
);
51 public function addDBDataOnce() {
52 if ( !$this->isWikitextNS( NS_MAIN
) ) {
53 // @todo cover the case of non-wikitext content in the main namespace
57 // Reset the search type back to default - some extensions may have
59 $this->setMwGlobals( [
60 'wgSearchType' => null,
61 'wgCapitalLinks' => true,
62 'wgCapitalLinkOverrides' => [
63 NS_CATEGORY
=> false // for testCompletionSearchMustRespectCapitalLinkOverrides
67 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
70 'This is not a talk page to the main page, see [[smithee]]'
72 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
73 $this->insertPage( 'Talk:Smithee', 'This article sucks.' );
74 $this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.' );
75 $this->insertPage( 'Another_page', 'This page also is unrelated.' );
76 $this->insertPage( 'Help:Help', 'Help me!' );
77 $this->insertPage( 'Thppt', 'Blah blah' );
78 $this->insertPage( 'Alan_Smithee', 'yum' );
79 $this->insertPage( 'Pages', 'are\'food' );
80 $this->insertPage( 'HalfOneUp', 'AZ' );
81 $this->insertPage( 'FullOneUp', 'AZ' );
82 $this->insertPage( 'HalfTwoLow', 'az' );
83 $this->insertPage( 'FullTwoLow', 'az' );
84 $this->insertPage( 'HalfNumbers', '1234567890' );
85 $this->insertPage( 'FullNumbers', '1234567890' );
86 $this->insertPage( 'DomainName', 'example.com' );
87 $this->insertPage( 'DomainName', 'example.com' );
88 $this->insertPage( 'Category:search is not Search', '' );
89 $this->insertPage( 'Category:Search is not search', '' );
92 protected function fetchIds( $results ) {
93 if ( !$this->isWikitextNS( NS_MAIN
) ) {
94 $this->markTestIncomplete( __CLASS__
. " does no yet support non-wikitext content "
95 . "in the main namespace" );
97 $this->assertTrue( is_object( $results ) );
100 foreach ( $results as $row ) {
101 $matches[] = $row->getTitle()->getPrefixedText();
104 # Search is not guaranteed to return results in a certain order;
105 # sort them numerically so we will compare simply that we received
106 # the expected matches.
112 public function testFullWidth() {
114 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
115 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
116 "Search for normalized from Half-width Upper" );
118 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
119 $this->fetchIds( $this->search
->searchText( 'az' ) ),
120 "Search for normalized from Half-width Lower" );
122 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
123 $this->fetchIds( $this->search
->searchText( 'AZ' ) ),
124 "Search for normalized from Full-width Upper" );
126 [ 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ],
127 $this->fetchIds( $this->search
->searchText( 'az' ) ),
128 "Search for normalized from Full-width Lower" );
131 public function testTextSearch() {
134 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
138 public function testWildcardSearch() {
139 $res = $this->search
->searchText( 'smith*' );
142 $this->fetchIds( $res ),
143 "Search with wildcards" );
145 $res = $this->search
->searchText( 'smithson*' );
148 $this->fetchIds( $res ),
149 "Search with wildcards must not find unrelated articles" );
151 $res = $this->search
->searchText( 'smith* smithee' );
154 $this->fetchIds( $res ),
155 "Search with wildcards can be combined with simple terms" );
157 $res = $this->search
->searchText( 'smith* "one who smiths"' );
160 $this->fetchIds( $res ),
161 "Search with wildcards can be combined with phrase search" );
164 public function testPhraseSearch() {
165 $res = $this->search
->searchText( '"smithee is one who smiths"' );
168 $this->fetchIds( $res ),
171 $res = $this->search
->searchText( '"smithee is who smiths"' );
174 $this->fetchIds( $res ),
175 "Phrase search is not sloppy, search terms must be adjacent" );
177 $res = $this->search
->searchText( '"is smithee one who smiths"' );
180 $this->fetchIds( $res ),
181 "Phrase search is ordered" );
184 public function testPhraseSearchHighlight() {
185 $phrase = "smithee is one who smiths";
186 $res = $this->search
->searchText( "\"$phrase\"" );
187 $match = $res->getIterator()->current();
188 $snippet = "A <span class='searchmatch'>" . $phrase . "</span>";
189 $this->assertStringStartsWith( $snippet,
190 $match->getTextSnippet( $res->termMatches() ),
191 "Highlight a phrase search" );
194 public function testTextPowerSearch() {
195 $this->search
->setNamespaces( [ 0, 1, 4 ] );
199 'Talk:Not Main Page',
201 $this->fetchIds( $this->search
->searchText( 'smithee' ) ),
205 public function testTitleSearch() {
211 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
215 public function testTextTitlePowerSearch() {
216 $this->search
->setNamespaces( [ 0, 1, 4 ] );
223 $this->fetchIds( $this->search
->searchTitle( 'smithee' ) ),
224 "Title power search" );
227 public function provideCompletionSearchMustRespectCapitalLinkOverrides() {
229 'Searching for "smithee" finds Smithee on NS_MAIN' => [
234 'Searching for "search is" will finds "search is not Search" on NS_CATEGORY' => [
236 'Category:search is not Search',
239 'Searching for "Search is" will finds "search is not Search" on NS_CATEGORY' => [
241 'Category:Search is not search',
248 * Test that the search query is not munged using wrong CapitalLinks setup
249 * (in other test that the default search backend can benefit from wgCapitalLinksOverride)
250 * Guard against regressions like T208255
251 * @dataProvider provideCompletionSearchMustRespectCapitalLinkOverrides
252 * @covers SearchEngine::completionSearch
253 * @covers PrefixSearch::defaultSearchBackend
254 * @param string $search
255 * @param string $expectedSuggestion
256 * @param int[] $namespaces
258 public function testCompletionSearchMustRespectCapitalLinkOverrides(
263 $this->search
->setNamespaces( $namespaces );
264 $results = $this->search
->completionSearch( $search );
265 $this->assertEquals( 1, $results->getSize() );
266 $this->assertEquals( $expectedSuggestion, $results->getSuggestions()[0]->getText() );
270 * @covers SearchEngine::getSearchIndexFields
272 public function testSearchIndexFields() {
274 * @var SearchEngine $mockEngine
276 $mockEngine = $this->getMockBuilder( SearchEngine
::class )
277 ->setMethods( [ 'makeSearchFieldMapping' ] )->getMock();
279 $mockFieldBuilder = function ( $name, $type ) {
281 $this->getMockBuilder( SearchIndexFieldDefinition
::class )->setConstructorArgs( [
286 $mockField->expects( $this->any() )->method( 'getMapping' )->willReturn( [
287 'testData' => 'test',
292 $mockField->expects( $this->any() )
294 ->willReturn( $mockField );
299 $mockEngine->expects( $this->atLeastOnce() )
300 ->method( 'makeSearchFieldMapping' )
301 ->willReturnCallback( $mockFieldBuilder );
303 // Not using mock since PHPUnit mocks do not work properly with references in params
304 $this->setTemporaryHook( 'SearchIndexFields',
305 function ( &$fields, SearchEngine
$engine ) use ( $mockFieldBuilder ) {
306 $fields['testField'] =
307 $mockFieldBuilder( "testField", SearchIndexField
::INDEX_TYPE_TEXT
);
311 $fields = $mockEngine->getSearchIndexFields();
312 $this->assertArrayHasKey( 'language', $fields );
313 $this->assertArrayHasKey( 'category', $fields );
314 $this->assertInstanceOf( SearchIndexField
::class, $fields['testField'] );
316 $mapping = $fields['testField']->getMapping( $mockEngine );
317 $this->assertArrayHasKey( 'testData', $mapping );
318 $this->assertEquals( 'test', $mapping['testData'] );
321 public function hookSearchIndexFields( $mockFieldBuilder, &$fields, SearchEngine
$engine ) {
322 $fields['testField'] = $mockFieldBuilder( "testField", SearchIndexField
::INDEX_TYPE_TEXT
);
326 public function testAugmentorSearch() {
327 $this->search
->setNamespaces( [ 0, 1, 4 ] );
328 $resultSet = $this->search
->searchText( 'smithee' );
329 // Not using mock since PHPUnit mocks do not work properly with references in params
330 $this->mergeMwGlobalArrayValue( 'wgHooks',
331 [ 'SearchResultsAugment' => [ [ $this, 'addAugmentors' ] ] ] );
332 $this->search
->augmentSearchResults( $resultSet );
333 foreach ( $resultSet as $result ) {
334 $id = $result->getTitle()->getArticleID();
335 $augmentData = "Result:$id:" . $result->getTitle()->getText();
336 $augmentData2 = "Result2:$id:" . $result->getTitle()->getText();
337 $this->assertEquals( [ 'testSet' => $augmentData, 'testRow' => $augmentData2 ],
338 $result->getExtensionData() );
342 public function addAugmentors( &$setAugmentors, &$rowAugmentors ) {
343 $setAugmentor = $this->createMock( ResultSetAugmentor
::class );
344 $setAugmentor->expects( $this->once() )
345 ->method( 'augmentAll' )
346 ->willReturnCallback( function ( SearchResultSet
$resultSet ) {
348 foreach ( $resultSet as $result ) {
349 $id = $result->getTitle()->getArticleID();
350 $data[$id] = "Result:$id:" . $result->getTitle()->getText();
354 $setAugmentors['testSet'] = $setAugmentor;
356 $rowAugmentor = $this->createMock( ResultAugmentor
::class );
357 $rowAugmentor->expects( $this->exactly( 2 ) )
358 ->method( 'augment' )
359 ->willReturnCallback( function ( SearchResult
$result ) {
360 $id = $result->getTitle()->getArticleID();
361 return "Result2:$id:" . $result->getTitle()->getText();
363 $rowAugmentors['testRow'] = $rowAugmentor;
366 public function testFiltersMissing() {
367 $availableResults = [];
368 foreach ( range( 0, 11 ) as $i ) {
369 $title = "Search_Result_$i";
370 $availableResults[] = $title;
371 // pages not created must be filtered
373 $this->editSearchResultPage( $title );
376 MockCompletionSearchEngine
::addMockResults( 'foo', $availableResults );
378 $engine = new MockCompletionSearchEngine();
379 $engine->setLimitOffset( 10, 0 );
380 $results = $engine->completionSearch( 'foo' );
381 $this->assertEquals( 5, $results->getSize() );
382 $this->assertTrue( $results->hasMoreResults() );
384 $engine->setLimitOffset( 10, 10 );
385 $results = $engine->completionSearch( 'foo' );
386 $this->assertEquals( 1, $results->getSize() );
387 $this->assertFalse( $results->hasMoreResults() );
390 private function editSearchResultPage( $title ) {
391 $page = WikiPage
::factory( Title
::newFromText( $title ) );
392 $page->doEditContent(
393 new WikitextContent( 'UTContent' ),
395 EDIT_NEW | EDIT_SUPPRESS_RC
399 public function provideDataForParseNamespacePrefix() {
413 'namespace prefix' => [
415 'query' => 'help:test',
417 [ 'test', [ NS_HELP
] ],
419 'accented namespace prefix with hook' => [
421 'query' => 'hélp:test',
424 [ 'test', [ NS_HELP
] ],
426 'accented namespace prefix without hook' => [
428 'query' => 'hélp:test',
433 'all with all keyword allowed' => [
435 'query' => 'all:test',
440 'all with all keyword disallowed' => [
442 'query' => 'all:test',
460 'all wins over namespace when first' => [
462 'query' => 'all:help:test',
465 [ 'help:test', null ]
467 'ns wins over all when first' => [
469 'query' => 'help:all:test',
472 [ 'all:test', [ NS_HELP
] ]
478 * @dataProvider provideDataForParseNamespacePrefix
479 * @param array $params
480 * @param array|false $expected
482 * @throws MWException
484 public function testParseNamespacePrefix( array $params, $expected ) {
485 $this->setTemporaryHook( 'PrefixSearchExtractNamespace', function ( &$namespaces, &$query ) {
486 if ( strpos( $query, 'hélp:' ) === 0 ) {
487 $namespaces = [ NS_HELP
];
488 $query = substr( $query, strlen( 'hélp:' ) );
493 if ( isset( $params['withAll'] ) && isset( $params['withHook'] ) ) {
494 $testSet[] = $params;
495 } elseif ( isset( $params['withAll'] ) ) {
496 $testSet[] = $params +
[ 'withHook' => true ];
497 $testSet[] = $params +
[ 'withHook' => false ];
498 } elseif ( isset( $params['withHook'] ) ) {
499 $testSet[] = $params +
[ 'withAll' => true ];
500 $testSet[] = $params +
[ 'withAll' => false ];
502 $testSet[] = $params +
[ 'withAll' => true, 'withHook' => true ];
503 $testSet[] = $params +
[ 'withAll' => true, 'withHook' => false ];
504 $testSet[] = $params +
[ 'withAll' => false, 'withHook' => false ];
505 $testSet[] = $params +
[ 'withAll' => true, 'withHook' => false ];
508 foreach ( $testSet as $test ) {
509 $actual = SearchEngine
::parseNamespacePrefixes( $test['query'],
510 $test['withAll'], $test['withHook'] );
511 $this->assertEquals( $expected, $actual, 'with params: ' . print_r( $test, true ) );